﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="RuleCacheHelper.cs" company="Scrum for Team System">
//   None
// </copyright>
// <summary>
//   Defines the StringIndex type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace ScrumForTeamSystem.EventService.AggregationService
{
    using System.Collections.Generic;

    using DataObjects;

    using Microsoft.TeamFoundation.WorkItemTracking.Client;

    /// <summary>
    /// Defines the RuleCacheHelper type.
    /// </summary>
    internal static class RuleCacheHelper
    {
        /// <summary>
        /// The rule functions hash table field.
        /// </summary>
        private static readonly IDictionary<AggregateRule, RuleFunctions> ruleFunctionsMap = new Dictionary<AggregateRule, RuleFunctions>();

        /// <summary>
        /// Tries the get the specfied rule functions.
        /// </summary>
        /// <param name="rule">The aggregate rule.</param>
        /// <param name="targetWorkItem">The target work item.</param>
        /// <param name="functions">The functions.</param>
        /// <returns><c>True</c> if the functions have been retieved; otherwise <c>false</c>.</returns>
        public static bool TryGetRuleFunctions(AggregateRule rule, WorkItem targetWorkItem, out RuleFunctions functions)
        {
            if (!ruleFunctionsMap.TryGetValue(rule, out functions))
            {
                if (!RuleFunctions.TryCreateRuleFunctions(rule, targetWorkItem, out functions))
                {
                    return false;
                }

                ruleFunctionsMap.Add(rule, functions);
            }

            return functions != null;
        }
    }
}
